package com.aisile.sellergoods.service.impl;
import java.util.List;

import com.aisile.entry.PageResult;
import com.aisile.mapper.TbSpecificationOptionMapper;
import com.aisile.pojo.TbSpecificationExample;
import com.aisile.pojo.TbSpecificationOption;
import com.aisile.pojo.TbSpecificationOptionExample;
import com.aisile.pojogroup.Specification;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.aisile.mapper.TbSpecificationMapper;
import com.aisile.pojo.TbSpecification;
import com.aisile.sellergoods.service.SpecificationService;


/**
 * 服务实现层
 * @author Administrator
 *
 */
@Service
public class SpecificationServiceImpl implements SpecificationService{

	@Autowired
	private TbSpecificationMapper specificationMapper;

	@Autowired
	private TbSpecificationOptionMapper specificationOptionMapper;
	/**
	 * 查询全部
	 */
	@Override
	public List<TbSpecification> findAll() {
		return specificationMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<TbSpecification> page=   (Page<TbSpecification>) specificationMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	public void add(TbSpecification specification) {
		specificationMapper.insert(specification);		
	}

	@Override
	public void add(Specification specification) {
		//入库：规格 规格选项列表
		TbSpecification tbSpecification=specification.getSpecification();
		List<TbSpecificationOption> tbSpecificationOptionList=specification.getSpecificationOptionList();
		//1.规格添加 数据库 获取规格id
		specificationMapper.insert(tbSpecification);
		//2.根据规格id 将规格选项添加到规格选项表中
		for (TbSpecificationOption option:tbSpecificationOptionList){
			option.setSpecId(tbSpecification.getId());
			specificationOptionMapper.insert(option);
		}

	}


	/**
	 * 修改
	 */
	@Override
	public void update(TbSpecification specification){
		specificationMapper.updateByPrimaryKey(specification);
	}

	@Override
	public void updateSpecification(Specification specification) {
		TbSpecification tbSpecification=specification.getSpecification();
		List<TbSpecificationOption> optionList=specification.getSpecificationOptionList();
		//规格修改
		specificationMapper.updateByPrimaryKey(tbSpecification);
		//删除上一次的规格选项
		TbSpecificationOptionExample example=new TbSpecificationOptionExample();
		example.createCriteria().andSpecIdEqualTo(tbSpecification.getId());
		specificationOptionMapper.deleteByExample(example);
		//添加这一次的规格选项
		for (TbSpecificationOption option:optionList){
			option.setSpecId(tbSpecification.getId());
			specificationOptionMapper.insert(option);
		}
	}

	/**
	 * 根据ID获取实体
	 * @param id
	 * @return
	 */
	@Override
	public TbSpecification findOne(Long id){
		return specificationMapper.selectByPrimaryKey(id);
	}

	@Override
	public Specification findSpecification(Long id) {
		//规格
		TbSpecification tbSpecification=specificationMapper.selectByPrimaryKey(id);
		//条件 ：根据id得到对应的规格选项集合
		TbSpecificationOptionExample example=new TbSpecificationOptionExample();
		example.createCriteria().andSpecIdEqualTo(id);
		//规格选项集合
		List<TbSpecificationOption> tbSpecificationOptionList=
				specificationOptionMapper.selectByExample(example);
		Specification specification=new Specification();
		specification.setSpecification(tbSpecification);
		specification.setSpecificationOptionList(tbSpecificationOptionList);
		return specification;
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Long[] ids) {
		for(Long id:ids){
			specificationMapper.deleteByPrimaryKey(id);
			//删除对应的规格选项
			TbSpecificationOptionExample example=new TbSpecificationOptionExample();
			example.createCriteria().andSpecIdEqualTo(id);
			specificationOptionMapper.deleteByExample(example);
		}
	}
	
	
		@Override
	public PageResult findPage(TbSpecification specification, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		TbSpecificationExample example=new TbSpecificationExample();
		TbSpecificationExample.Criteria criteria = example.createCriteria();
		
		if(specification!=null){			
						if(specification.getSpecName()!=null && specification.getSpecName().length()>0){
				criteria.andSpecNameLike("%"+specification.getSpecName()+"%");
			}
	
		}
		
		Page<TbSpecification> page= (Page<TbSpecification>)specificationMapper.selectByExample(example);		
		return new PageResult(page.getTotal(), page.getResult());
	}
	
}
